package com.zr.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.zr.dao.CarDao;
import com.zr.model.TCar;
import com.zr.model.TCarstate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 重写车辆的dao层编写具体实现
 * @author Administrator
 *
 */
@Repository("carDao")
public class CarDaoImpl<T> extends BaseDaOImpl<T> implements CarDao<T>{

	@Override
	public long showCarCount() {
		// TODO Auto-generated method stub
		//查询车辆总数的hql语句
		String hql = "select count(*) from TCar";
		//将查出来的数据放在list中
		List<Object> obj = this.getCurrentSession().createQuery(hql).list();
		//获取数据
		return (long) obj.get(0);
	}

	@Override
	public List<TCar> showCarMsg(int page, int rows) {
		// TODO Auto-generated method stub
		 //查询车辆信息的sql语句
		 String sql = "select cid,cname,cvolume,t_car.csid,csname from t_car,t_carstate where t_car.csid=t_carstate.csid limit :start,:scount";
		 //将查出来的数据转成map
		 Query query=this.getCurrentSession().createSQLQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		 //指定当前分页查询的起始位置和行数
		 query.setInteger("start",(page-1)*rows);
	     query.setInteger("scount",rows);
	     //将Query对象query转换成集合,并且把该集合赋值给一个新的集合
	      List<TCar> cars = query.list();
		  return cars;
	}
	@Override
	public JSONArray findCarCsnameMsg() {
		// TODO Auto-generated method stub
		//声明一个map数据集合
		Map<String, String> map = new HashMap<String,String>();
		//声明一个jso数组
		JSONArray jso=new JSONArray();
		//查询车辆状态信息的hql语句
		String hql="from TCarstate";
		//查询hql语句
		Query query=this.getCurrentSession().createQuery(hql);
		//将查询出来的hql数据放入list中
		List<TCarstate> tcarstate=query.list();
		for (TCarstate tc : tcarstate) {
			//获得状态的id和名字
			String CSid=String.valueOf(tc.getCsid());
			String CSname=tc.getCsname();
			//将状态id和名字放入mao集合中
			map.put("id", CSid);
			map.put("text", CSname);
			//将这个集合放入jso数组中
			jso.add(map);
		}
		//返回jso数组
		return jso;
	}
	
	@Override
	public boolean addCarMsg(String cname, String cvolume,TCarstate TCarstate) {
		// TODO Auto-generated method stub
		boolean flag =false;
		TCar car = new TCar();
		car.setCname(cname);
		car.setCvolume(cvolume);
		car.setTCarstate(TCarstate);
		this.getCurrentSession().save(car);
		return flag;
	}

	@Override
	public TCarstate findTCarstateCsid(int csid) {
		// TODO Auto-generated method stub
		TCarstate TCarstate = (TCarstate) this.getCurrentSession().get(TCarstate.class, Integer.valueOf(csid));
		return TCarstate;
	}

	@Override
	public void deleteCarMsg(int cid) {
		// TODO Auto-generated method stub
		//通过TCar查询cid
		TCar car = (TCar) this.getCurrentSession().get(TCar.class, Integer.valueOf(cid));
		//删除当前的cid
		this.getCurrentSession().delete(car);
	}

	@Override
	public void updateCarMsg(int cid, String cname, String cvolume, TCarstate TCarstate) {
		// TODO Auto-generated method stub
		TCar car = (TCar) this.getCurrentSession().get(TCar.class, Integer.valueOf(cid));
		car.setCname(cname);
		car.setCvolume(cvolume);
		car.setTCarstate(TCarstate);
		this.getCurrentSession().update(car);
	}





}
